Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

321
Views
convert slightly complex "if" statement to ternary operator

I have the following code from a project:

setTweetLibrary((curr) => {
    if (!curr[0] || (curr[0] && curr[0].id !== saved_tweets[0].id)) {
        console.log("update");
        return saved_tweets;
    } else {
        console.log("not update");
        return curr;
    }
});

I would like to convert this to a ternary operator with a question mark ("?") to make it neater, but I can't figure out how. Can anyone advise? Thank you!

I think I'm just really confused on how the syntax of ternary operators with multiple conditions and values

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

As ternary it would look like this:

setTweetLibrary((curr) => {
      return !curr[0] || (curr[0] && curr[0].id !== saved_tweets[0].id)
        ? saved_tweets
        : curr;
});

Note that there is no more place for console.logs. But they seem to be debug anyway.

about 4 years ago · Juan Pablo Isaza Report

0

The ternary operator is not a generic replacement for an if statement, only for choosing between expressions that are going to the same destination.

If we take out the logging, both branches are returning a variable:

if (!curr[0] || (curr[0] && curr[0].id !== saved_tweets[0].id)) {
    return saved_tweets;
} else {
    return curr;
}

The fact that the condition is a long expression doesn't make any difference, it's still equivalent to this:

if (some_condition_goes_here) {
    return saved_tweets;
} else {
    return curr;
}

In that case, you could use a ternary operator to select between the two - move the return to where the if is, and the ? and : before the two values. Then put the whole thing in parentheses to make sure the whole thing is calculated before return:

return (
    (some_condition_goes_here)
    ? saved_tweets
    : curr
);

Specifically:

return (
    (!curr[0] || (curr[0] && curr[0].id !== saved_tweets[0].id))
    ? saved_tweets
    : curr
);

It might be possible to add the logging in there as well, but it will be much harder to read than just using a normal if statement, so if you want that - or any other logic - just stick with if...else.

about 4 years ago · Juan Pablo Isaza Report

0

This works

 return (!curr[0] || (curr[0] && curr[0].id !== saved_tweets[0].id) ? saved_tweets : curr)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!